/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.economy.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

import ar.uba.fi.posgrado.common.dao.AbstractDAO;
import ar.uba.fi.posgrado.common.dao.DAOException;
import ar.uba.fi.posgrado.common.dao.NoSuchEntityException;
import ar.uba.fi.posgrado.economy.dao.DiscountDAO;
import ar.uba.fi.posgrado.economy.model.Discount;

/**
 * Implementacion de {@linkplain DiscountDAO discount DAO}
 * 
 * @author nicolas.gonzalez
 * 
 * @see AbstractDAO
 * @see DiscountDAO
 */
public class DiscountDAOImpl extends AbstractDAO<Discount> implements DiscountDAO {

    /**
     * @see ar.uba.fi.posgrado.common.dao.AbstractDAO#getClazz()
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Class getClazz() {
        return Discount.class;
    }

    /**
     * @see ar.uba.fi.posgrado.economy.dao.DiscountDAO#findByDescription(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public Discount findByDescription(final String description)
            throws NoSuchEntityException, DAOException {
        Discount response = null;
        try {
            response = (Discount) this.getHibernateTemplate().execute(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session) {
                            Criteria criteria = session
                                    .createCriteria(Discount.class);
                                    criteria.add(Expression.eq("description",
                                    description));
                            return criteria.uniqueResult();
                        }
                    });

            if (response == null) {
                // Necesario ya q el criteria no falla al no encontrar
                throw new NoSuchEntityException(
                        "No existe intancia de Descuento"
                                + ", con description: " + description);
            }
        } catch (ObjectRetrievalFailureException e) {
            throw new NoSuchEntityException("No existe intancia de Descuento"
                    + ", con description: " + description, e);
        } catch (DataAccessException e) {
            throw new DAOException(
                    "Excepcion al buscar una intancia de Descuento "
                            + ", con description: " + description
                            + ". Mensaje: " + e.getMessage(), e);
        }

        return response;
    }

}
